package cn.iocoder.yudao.module.enterprise.service.account;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.enterprise.controller.admin.account.vo.AccountCreateReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.account.vo.AccountPageReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.account.vo.AccountUpdateReqVO;
import cn.iocoder.yudao.module.enterprise.convert.account.AccountConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.account.AccountDO;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.account.AccountSourceDO;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.model.ModelEmissionDO;
import cn.iocoder.yudao.module.enterprise.dal.mysql.account.AccountMapper;
import cn.iocoder.yudao.module.enterprise.dal.mysql.account.AccountSourceMapper;
import cn.iocoder.yudao.module.enterprise.dal.mysql.model.ModelEmissionMapper;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.enterprise.enums.ErrorCodeConstants.ACCOUNT_NOT_EXISTS;

/**
 * 碳排放核算 Service 实现类
 *
 * @author Steven
 */
@Service
@Validated
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountMapper accountMapper;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private ModelEmissionMapper modelEmissionMapper;
    @Resource
    private AccountSourceMapper accountSourceMapper;

    @Override
    public Long createAccount(AccountCreateReqVO createReqVO) {
        // 插入碳排放核算表
        AccountDO account = AccountConvert.INSTANCE.convert(createReqVO);
        accountMapper.insert(account);

        // 插入碳排放核算的排放源表
        List<ModelEmissionDO> modelEmissionList = modelEmissionMapper.selectListByModelId(createReqVO.getModelId());
        if (!CollUtil.isEmpty(modelEmissionList)) {
            List<AccountSourceDO> accountSourceList = new ArrayList<>();
            for (ModelEmissionDO modelEmission : modelEmissionList) {
                AccountSourceDO accountSource = new AccountSourceDO();
                accountSource.setSourceId(modelEmission.getEmissionId())
                        .setAccountId(account.getId())
                        .setDataValue(0D);
                accountSourceList.add(accountSource);
            }
            accountSourceMapper.insertBatch(accountSourceList);
        }

        // 返回
        return account.getId();
    }

    @Override
    public void updateAccount(AccountUpdateReqVO updateReqVO) {
        // 校验存在
        validateAccountExists(updateReqVO.getId());
        // 更新
        AccountDO updateObj = AccountConvert.INSTANCE.convert(updateReqVO);
        accountMapper.updateById(updateObj);
    }

    @Override
    public void deleteAccount(Long id) {
        // 校验存在
        validateAccountExists(id);
        // 删除
        accountMapper.deleteById(id);
    }

    private void validateAccountExists(Long id) {
        if (accountMapper.selectById(id) == null) {
            throw exception(ACCOUNT_NOT_EXISTS);
        }
    }

    @Override
    public AccountDO getAccount(Long id) {
        return accountMapper.selectById(id);
    }

    @Override
    public PageResult<AccountDO> getAccountPage(AccountPageReqVO pageReqVO) {
        PageResult<AccountDO> page = accountMapper.selectPage(pageReqVO);
        List<AccountDO> list = page.getList();
        // 获取更新者名字
        list = list.stream().map(accountDO -> {
            AdminUserRespDTO userRespDTO = adminUserApi.getUser(Long.valueOf(accountDO.getUpdater())).getCheckedData();
            accountDO.setUpdater(userRespDTO.getNickname());
            return accountDO;
        }).collect(Collectors.toList());
        page.setList(list);
        return page;
    }

    @Override
    public List<AccountDO> getAccountList(Collection<Long> ids) {
        return accountMapper.selectBatchIds(ids);
    }

}
